import Mock from 'mockjs'
import type { MockMethod } from 'vite-plugin-mock'
import type { LoginApiParams } from '../src/type/ApiType'

const Random = Mock.Random

export default [
  {
    url: '/api/login',
    method: 'post',
    response: (data: { body: LoginApiParams}) => {
      // console.log(data.body)
      const name = data.body.username
      return Mock.mock({
        code: 0,
        msg: '登录成功',
        data: {
          'userId|1-10000': 10000,
          roled: 2,
          uname: name,
          token: Random.string(15, 20),
          avatar: Random.image(
            '50x50',
            '#' + Math.random().toString(16).slice(2, 8),
            '#FFF',
            'png',
            name.slice(0, 1),
          ),
          routers: [
            '/info/dashoard',
            '/sys/user',
            '/sys/role',
            '/sys/auths',
            '/film/list',
            '/film/create',
          ],
        },
      })
    },
  },
  {
    url: '/api/users/jurisdiction',
    method: 'post',
    response: () => {
      return {
        code: 0,
        msg: 'ok',
        data: [
          {
            id: 1,
            name: '信息汇总',
            ename: 'info',
            pid: 0,
            menu: 1,
            route: '',
            icon: 'PieChartOutlined',
            children: [
              {
                id: 10,
                name: '图形汇总',
                ename: 'dashoard',
                pid: 1,
                menu: 1,
                route: '/info/dashoard',
                icon: 'DesktopOutlined'
              }
            ]
          },
          {
            id: 2,
            name: '后台设置',
            ename: 'sys',
            pid: 0,
            menu: 1,
            route: '',
            icon: 'UserOutlined',
            children: [
              {
                id: 20,
                name: '用户列表',
                ename: 'user',
                pid: 2,
                menu: 1,
                route: '/sys/user',
                icon: 'TeamOutlined'
              },
              {
                id: 21,
                name: '地图',
                ename: 'role',
                pid: 2,
                menu: 1,
                route: '/sys/role',
                icon: 'FileOutlined'
              },
              {
                id: 22,
                name: '客服聊天',
                ename: 'role',
                pid: 2,
                menu: 1,
                route: '/sys/auths',
                icon: 'QqOutlined'
              }
            ]
          },
          {
            id: 3,
            name: '电影',
            ename: 'film',
            pid: 0,
            menu: 1,
            route: '',
            icon: 'CodeSandboxOutlined',
            children: [
              {
                id: 30,
                name: '电影列表',
                ename: 'filmlist',
                pid: 1,
                menu: 1,
                route: '/film/list',
                icon: 'AliyunOutlined'
              },
              {
                id: 31,
                name: '电影创建',
                ename: 'filmcreate',
                pid: 1,
                menu: 1,
                route: '/film/create',
                icon: 'DribbbleOutlined'
              }
            ]
          },
        ]
      }
    },
  },
  {
    url: '/api/info/dashoard',
    method: 'post',
    response: () => {
      return Mock.mock({
        code: 0,
        msg: '成功',
        data: {
          'result|5': [
            {
              name() {
                return Random.cname()
              },
              score: {
                语文: { 'value|30-150': 150, limit: 150 },
                数学: { 'value|30-150': 150, limit: 150 },
                英语: { 'value|30-150': 150, limit: 150 },
                地理: { 'value|30-100': 100, limit: 100 },
                历史: { 'value|30-100': 100, limit: 100 },
                物理: { 'value|30-100': 100, limit: 100 },
              },
            },
          ],
        },
      })
    },
  },
  {
    url: '/api/user/list',
    method: 'get',
    response: ({ query }: { query: { page: number, size: number } }) => {
      const page = query.page ? Number(query.page) : 1
      const size = query.size ? Number(query.size) : 10
      // mockjs来生成假数据
      const users = Mock.mock({
        [`users|${size}`]: [
          {
            'id|+1': (page - 1) * size,
            username: '@name',
            realname: '@cname',
            'age|18-50': 18,
            'sex|1': [1, 2],
            birthday: '@date',
            avatar:
              'https://img2.baidu.com/it/u=3134235566,1012432277&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
            city: () => Mock.mock('@city(true)'),
            address: () => Mock.mock('@county(true)'),
          },
        ],
      })
      return {
        code: 0,
        msg: 'ok',
        data: {
          ...users,
          total: 100,
        },
      }
    },
  },
  {
    url: '/api/user/delete',
    method: 'delete',
    response: ({ userId }: { userId: number }) => {
      console.log('删除成功', userId)
      return {
        code: 0,
        msg: '删除成功',
      }
    },
  },
] as MockMethod[]
